package edu.buaa.act.model;

import com.graphhopper.matching.GPXFile;
import com.graphhopper.util.GPXEntry;
import edu.buaa.act.model.tsc.MyMapMatching;
import edu.buaa.act.model.tsc.MyMapMatching.TrajectoryRoadEntry;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by T7810 on 2017/4/10.
 */
public class Trajectory
{
    private int carID;
    private List<GPXEntry> gpsEntries = new ArrayList<>();
    private List<TrajectoryRoadEntry> roads = new ArrayList<>();
    private boolean isHistory;
    private boolean isSample;
    private double length;
    private int estimateTravelTime;

    public Trajectory()
    {
    }

    public Trajectory(List<GPXEntry> inputGPXEntries)
    {
        this.gpsEntries = inputGPXEntries;
    }

    public int getCarID()
    {
        return carID;
    }

    public void setCarID(int carID)
    {
        this.carID = carID;
    }

    public List<GPXEntry> getGpsEntries()
    {
        return gpsEntries;
    }

    public void addGpsEntry(GPXEntry gpsEntry)
    {
        this.gpsEntries.add(gpsEntry);
    }

    public boolean isHistory()
    {
        return isHistory;
    }

    public void setHistory(boolean history)
    {
        isHistory = history;
    }

    public boolean isSample()
    {
        return isSample;
    }

    public void setSample(boolean sample)
    {
        isSample = sample;
    }

    public List<TrajectoryRoadEntry> getRoads()
    {
        return roads;
    }

    // return time in seconds.
    public int realTravelTime()
    {
        if(gpsEntries.size()>1)
        {
            long departureTime = gpsEntries.get(0).getTime();
            long arriveTime = gpsEntries.get(gpsEntries.size() - 1).getTime();
            return (int) ((arriveTime - departureTime) / 1000);
        }else{
            return 0;
        }
    }

    public void setRoads(List<TrajectoryRoadEntry> roads)
    {
        this.roads = roads;
    }

    public void toFile(File outputFile)
    {
        new GPXFile(this.gpsEntries).doExport(outputFile.getAbsolutePath());
    }

    public double getLength()
    {
        return length;
    }

    public void setLength(double length)
    {
        this.length = length;
    }

    public Query toQuery()
    {
        List<TrajectoryRoadEntry> roads = this.getRoads();
        List<Integer> roadIDList = new ArrayList<>();
        for(TrajectoryRoadEntry r:roads){
            roadIDList.add(r.edgeID);
        }
        return new Query(roadIDList, this.getGpsEntries().get(0).getTime());
    }

    public int getEstimateTravelTime()
    {
        return estimateTravelTime;
    }

    public void setEstimateTravelTime(int estimateTravelTime)
    {
        this.estimateTravelTime = estimateTravelTime;
    }

    public double getDirectLength()
    {
        if(gpsEntries.size()>1)
        {
            double len = 0;
            for (int i = 1; i < gpsEntries.size(); i++)
            {
                GPXEntry pre = gpsEntries.get(i - 1);
                GPXEntry cur = gpsEntries.get(i);
                len += MyMapMatching.distanceCalc.calcDist(pre.getLat(), pre.getLon(), cur.getLat(), cur.getLon());
            }
            return len;
        }
        return 0;
    }
}
